# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Serie, SerieVideo, Video
from datetime import datetime


class List(BaseController):

    def get(self):
        serie = Serie.objects
        state = self.int_argument('state', 0)
        serie = serie.filter(state=state)
        serie = serie.order_by('show_order')
        number_per_page = self.int_argument('pageSize', 50)
        paginator = self.pagination(serie.all(), number_per_page=number_per_page)
        return self.render_json(data=paginator)


class Detail(BaseController):

    def get(self):
        id = self.int_argument('id', 0)
        if id <= 0:
            return self.render_json(code=1001, msg='参数错误')
        serie = Serie.objects.filter(id=id).first()
        
        return self.render_json(data=serie.to_dict())


class ListAll(BaseController):

    def get(self):
        series = Serie.objects.filter(state=0).order_by('show_order').all()
        data = [ {'label': e.name, 'value': e.id} for e in series ]
        return self.render_json(data={'series': data})


class BaseSerieController(BaseController):

    def set_serie_properties(self, serie, data):
        serie.name = data.get('name')
        serie.show_order = data.get('show_order')
        serie.state = data.get('state')
        serie.create_time = datetime.now()
        serie.save()
        return serie

class CreateSerie(BaseSerieController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='元素名称不能为空')
        serie = Serie()
        serie = self.set_serie_properties(serie, data)
        return self.render_json(data=serie.to_dict())


class SerieDelController(BaseSerieController):

    def post(self, id):
        serie = Serie.objects.filter(id=id).first()
        if not serie:
            return self.render_json(code=1001, msg='系列不存在')
        serie.state = 1
        serie.save()
        return self.render_json(msg='删除成功')


class UpdateSerie(BaseSerieController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='系列名称不能为空')
        serie_id = data.get('id', -1)
        if serie_id <= 0:
            return self.render_json(code=1001, msg='系列ID不能为空')
        serie = Serie.objects.filter(id=serie_id).first()
        if not serie:
            return self.render_json(code=1001, msg='系列不存在')
        serie = self.set_serie_properties(serie, data)
        return self.render_json(data=serie.to_dict())


class ListSerieVideo(BaseController):

    def get(self):
        serie = SerieVideo.objects
        serie_id = self.int_argument('serie_id', -1)
        if serie_id <= 0:
            return self.render_json(code=1001, msg='系列ID不能为空')
        serie = serie.filter(serie_id=serie_id)
        state = self.int_argument('state', 0)
        serie = serie.filter(state=state).select_related('video', 'serie')
        serie = serie.order_by('show_order')
        number_per_page = self.int_argument('pageSize', 50)
        paginator = self.pagination(serie.all(), number_per_page=number_per_page)
        return self.render_json(data=paginator)


class BaseSerieVideoController(BaseController):

    def set_serie_properties(self, serie, data, video):
        serie.name = data.get('name')
        serie.show_order = data.get('show_order')
        serie.state = data.get('state')
        serie.img_url = data.get('img_url')
        serie.video = video
        serie.serie_id = data.get('serie_id')
        serie.save()
        return serie


class CreateSerieVideo(BaseSerieVideoController):

    def post(self):
        data = json.loads(self.request.body)
        vid = int(data.get('video_id', -1))
        if vid <= 0:
            return self.render_json(code=1001, msg='视频ID不能为空')
        video = Video.objects.filter(id=vid).first()
        if not video:
            return self.render_json(code=1001, msg='视频不存在')
        serie_video = SerieVideo()
        serie_video = self.set_serie_properties(serie_video, data, video)
        return self.render_json(data=serie_video.to_dict())


class UpdateSerieVideo(BaseSerieVideoController):

    def post(self):
        data = json.loads(self.request.body)
        serie_video_id = data.get('id', -1)
        if serie_video_id <= 0:
            return self.render_json(code=1001, msg='系列视频ID不能为空')
        serie = SerieVideo.objects.filter(id=serie_video_id).first()
        vid = int(data.get('video_id', -1))
        if vid <= 0:
            return self.render_json(code=1001, msg='视频ID不能为空')
        video = Video.objects.filter(id=vid).first()
        if not video:
            return self.render_json(code=1001, msg='视频不存在')
        if not serie:
            return self.render_json(code=1001, msg='系列不存在')
        serie = self.set_serie_properties(serie, data, video)
        return self.render_json(data=serie.to_dict())

class SerieVideoDelController(BaseSerieController):

    def post(self, id):
        serie = SerieVideo.objects.filter(id=id).first()
        if not serie:
            return self.render_json(code=1001, msg='系列视频不存在')
        serie.state = 1
        serie.save()
        return self.render_json(msg='删除成功')
